package com.kky.pp.view.upImage

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.kky.pp.R
import com.kky.pp.bean.UserPhotoBean
import com.kky.pp.util.CollectionUtils
import com.kky.pp.util.DensityUtil
import com.makeramen.roundedimageview.RoundedImageView


class UpImageView : FrameLayout {
    constructor(context: Context) : this(context = context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    private var showCoverBg: Boolean=true
    private var showSmallRadius: Boolean=true
    var photoSelectPosition = 0
    private var imageClickListener: ImageClickListener? = null
    private var ivImageOne: RoundedImageView? = null
    private var ivImageTwo: RoundedImageView? = null
    private var ivImageThree: RoundedImageView? = null
    private var ivImageFour: RoundedImageView? = null
    private var ivImageFive: RoundedImageView? = null
    private var ivImageSix: RoundedImageView? = null
    private var ivImageOneClose: ImageView? = null
    private var ivImageTwoClose: ImageView? = null
    private var ivImageThreeClose: ImageView? = null
    private var ivImageFourClose: ImageView? = null
    private var ivImageFiveClose: ImageView? = null
    private var ivImageSixClose: ImageView? = null
    private var tvCover: TextView? = null
    private val imageViews = arrayListOf<RoundedImageView>()
    private val imageViewCloses = arrayListOf<ImageView>()
    var userImages = arrayListOf<UserPhotoBean>()

    @SuppressLint("MissingInflatedId", "CustomViewStyleable")
    fun initView(context: Context, attrs: AttributeSet?) {
        val view: View = LayoutInflater.from(context).inflate(R.layout.view_up_image, this)
        val obtainStyledAttributes =
            context.obtainStyledAttributes(attrs, R.styleable.UploadImageView)
        showCoverBg = obtainStyledAttributes.getBoolean(R.styleable.UploadImageView_showCoverBg, false)
        showSmallRadius = obtainStyledAttributes.getBoolean(R.styleable.UploadImageView_showSmallRadius, false)

        ivImageOne = view.findViewById(R.id.ivImageOne)
        imageViews.add(ivImageOne!!)
        ivImageTwo = view.findViewById(R.id.ivImageTwo)
        imageViews.add(ivImageTwo!!)
        ivImageThree = view.findViewById(R.id.ivImageThree)
        imageViews.add(ivImageThree!!)
        ivImageFour = view.findViewById(R.id.ivImageFour)
        imageViews.add(ivImageFour!!)
        ivImageFive = view.findViewById(R.id.ivImageFive)
        imageViews.add(ivImageFive!!)
        ivImageSix = view.findViewById(R.id.ivImageSix)
        imageViews.add(ivImageSix!!)

        ivImageOneClose = view.findViewById(R.id.ivImageOneClose)
        imageViewCloses.add(ivImageOneClose!!)
        ivImageTwoClose = view.findViewById(R.id.ivImageTwoClose)
        imageViewCloses.add(ivImageTwoClose!!)
        ivImageThreeClose = view.findViewById(R.id.ivImageThreeClose)
        imageViewCloses.add(ivImageThreeClose!!)
        ivImageFourClose = view.findViewById(R.id.ivImageFourClose)
        imageViewCloses.add(ivImageFourClose!!)
        ivImageFiveClose = view.findViewById(R.id.ivImageFiveClose)
        imageViewCloses.add(ivImageFiveClose!!)
        ivImageSixClose = view.findViewById(R.id.ivImageSixClose)
        imageViewCloses.add(ivImageSixClose!!)

        tvCover = view.findViewById(R.id.tvCover)
        initListener()
    }

    fun addImageClickListener(imageClickListener: ImageClickListener) {
        this.imageClickListener = imageClickListener
    }

    private fun initListener() {
        imageViews.stream().forEach {
            it.setOnClickListener {
                photoSelectPosition = it.tag as Int
                imageClickListener?.imageClick(true)
            }
        }
        imageViewCloses.stream().forEach { it ->
            it.setOnClickListener {
                photoSelectPosition = it.tag as Int
                imageClickListener?.imageClick(false)
            }
        }
    }

    fun upDataItemImage(position: Int) {
        if (position >= userImages.size) {
            return
        }
        val userPhotoBean = userImages[position]
        val url = if (TextUtils.isEmpty(userPhotoBean.imageLoadUrl)) {
            userPhotoBean.imageUrl
        } else {
            userPhotoBean.imageLoadUrl
        }
        val imageViewClose = imageViewCloses[position]
        val imageView = imageViews[position]
        if (!TextUtils.isEmpty(url)) {
            Glide.with(imageView)
                .load(url)
                .placeholder(R.mipmap.icon_image_loading)
                .into(imageView)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageViewClose.isVisible = position != 0
        } else {
            if(position != 0) {
                imageView.background =if (showSmallRadius) ContextCompat.getDrawable(context, R.drawable.shape_283040_radius_12) else ContextCompat.getDrawable(context, R.drawable.shape_283040_radius_25)
                imageView.setImageResource(R.mipmap.icon_min_add)
            }else{
                if (showCoverBg){
                    imageView.background = ContextCompat.getDrawable(context, R.drawable.shape_stroke_ff6bfa_radius_25)
                }
                imageView.setImageResource(R.mipmap.icon_min_add_pink)
            }
            imageView.scaleType = ImageView.ScaleType.CENTER_INSIDE
            imageViewClose.isVisible = false
        }
    }

    fun setData(userImages: ArrayList<UserPhotoBean>) {
        if (!CollectionUtils.isNotEmpty(userImages)){
            return
        }
        this.userImages = userImages
        val residueNum = 6 - userImages.size
        for (num in 0 until residueNum) {
            userImages.add(UserPhotoBean("", "", "", false))
        }
        for (position in 0 until userImages.size) {
            val imageView = imageViews[position]
            val imageViewClose = imageViewCloses[position]
            imageView.tag = position
            imageView.cornerRadius=if (showSmallRadius) DensityUtil.dp2pxF(context,12f) else DensityUtil.dp2pxF(context,25f)
            upDataItemImage(position)
            imageViewClose.tag = position
            imageView.background =if (showSmallRadius) ContextCompat.getDrawable(context, R.drawable.shape_283040_radius_12) else ContextCompat.getDrawable(context, R.drawable.shape_283040_radius_25)

        }
        if (showCoverBg) {
            ivImageOne!!.setBackgroundResource(R.drawable.shape_stroke_ff6bfa_radius_25)
        }
//        imageViews[0].setBackgroundResource()
//        tvCover?.isVisible = !TextUtils.isEmpty(userImages[0].imageUrl)
    }

}